//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Security;
using DotNetNuke.Security.Permissions;
using DotNetNuke.UI.Utilities;
namespace DotNetNuke.Modules.Admin.Tabs
{
	partial class Tabs : Entities.Modules.PortalModuleBase
	{
		protected bool IsAdminRole {
			get { return PortalSecurity.IsInRole(PortalSettings.AdministratorRoleName); }
		}
		protected List<TabInfo> TabsList {
			get {
				if (chkDisplayHost.Checked) {
					return TabController.GetPortalTabs(Null.NullInteger, Null.NullInteger, false, true, false, true);
				} else {
					return TabController.GetPortalTabs(PortalId, Null.NullInteger, false, true, false, true);
				}
			}
		}
		private void AddTab()
		{
			if (lstTabs.SelectedIndex != -1) {
				TabInfo objTab = TabsList[lstTabs.SelectedIndex];
				Response.Redirect(Common.Globals.NavigateURL(objTab.TabID, "Tab", "action=add", "returntabid=" + TabId.ToString()), true);
			} else {
				Response.Redirect(Common.Globals.NavigateURL(PortalSettings.HomeTabId, "Tab", "action=add", "returntabid=" + TabId.ToString()), true);
			}
		}
		private void DeleteTab()
		{
			if (lstTabs.SelectedIndex != -1) {
				TabInfo objTab = TabsList[lstTabs.SelectedIndex];
				if (!TabController.IsSpecialTab(objTab.TabID, PortalSettings)) {
					TabController.DeleteTab(objTab.TabID, PortalSettings, UserId);
					Response.Redirect(Common.Globals.NavigateURL(TabId), true);
				} else {
					UI.Skins.Skin.AddModuleMessage(this, Services.Localization.Localization.GetString("DeleteSpecialPage", this.LocalResourceFile), UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
				}
			}
		}
		private void EditTab()
		{
			if (lstTabs.SelectedIndex != -1) {
				TabInfo objTab = TabsList[lstTabs.SelectedIndex];
				Response.Redirect(Common.Globals.NavigateURL(objTab.TabID, "Tab", "action=edit", "returntabid=" + TabId.ToString()), true);
			}
		}
		private void CheckSecurity()
		{
			if ((!TabPermissionController.HasTabPermission("CONTENT")) && !ModulePermissionController.HasModulePermission(this.ModuleConfiguration.ModulePermissions, "CONTENT, EDIT")) {
				Response.Redirect(Common.Globals.NavigateURL("Access Denied"), true);
			}
		}
		private void ViewTab()
		{
			if (lstTabs.SelectedIndex != -1) {
				TabController objTabs = new TabController();
				TabInfo objTab = objTabs.GetTab(TabsList[lstTabs.SelectedIndex].TabID, PortalId, false);
				if (objTab != null) {
					Response.Redirect(Common.Globals.NavigateURL(objTab.TabID), true);
				}
			}
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			chkDisplayHost.CheckedChanged += new EventHandler(chkDisplayHost_CheckedChanged);
			cmdAdd.Click += new ImageClickEventHandler(cmdAdd_Click);
			cmdDelete.Click += new ImageClickEventHandler(cmdDelete_Click);
			cmdEdit.Click += new ImageClickEventHandler(cmdEdit_Click);
			cmdView.Click += new ImageClickEventHandler(cmdView_Click);
			cmdTop.Click += new ImageClickEventHandler(Move_Click);
			cmdUp.Click += new ImageClickEventHandler(Move_Click);
			cmdDown.Click += new ImageClickEventHandler(Move_Click);
			cmdBottom.Click += new ImageClickEventHandler(Move_Click);
			cmdLeft.Click += new ImageClickEventHandler(Move_Click);
			cmdRight.Click += new ImageClickEventHandler(Move_Click);

			try {
				CheckSecurity();
				pnlHost.Visible = UserInfo.IsSuperUser;
				if (Page.IsPostBack == false) {
					if (!string.IsNullOrEmpty(Request.QueryString["isHost"])) {
						chkDisplayHost.Checked = bool.Parse(Request.QueryString["isHost"]);
					}
					lstTabs.DataSource = TabsList;
					lstTabs.DataBind();
					if (!string.IsNullOrEmpty(Request.QueryString["selecttabid"])) {
						if (lstTabs.Items.FindByValue(Request.QueryString["selecttabid"]) != null) {
							lstTabs.Items.FindByValue(Request.QueryString["selecttabid"]).Selected = true;
						}
					}
				}
				ClientAPI.AddButtonConfirm(cmdDelete, Services.Localization.Localization.GetString("DeleteItem"));
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void Move_Click(object sender, ImageClickEventArgs e)
		{
			try {
				if (lstTabs.SelectedIndex != -1) {
					TabInfo objTab = TabsList[lstTabs.SelectedIndex];
					TabController objTabs = new TabController();
					switch (((ImageButton)sender).CommandName) {
						case "top":
							objTabs.MoveTab(objTab, TabMoveType.Top);
							break;
						case "bottom":
							objTabs.MoveTab(objTab, TabMoveType.Bottom);
							break;
						case "up":
							objTabs.MoveTab(objTab, TabMoveType.Up);
							break;
						case "down":
							objTabs.MoveTab(objTab, TabMoveType.Down);
							break;
						case "left":
							objTabs.MoveTab(objTab, TabMoveType.Promote);
							break;
						case "right":
							objTabs.MoveTab(objTab, TabMoveType.Demote);
							break;
					}
					Response.Redirect(Common.Globals.NavigateURL(TabId, "", "selecttabid", objTab.TabID.ToString(), "isHost", chkDisplayHost.Checked.ToString().ToLowerInvariant()), true);
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected void chkDisplayHost_CheckedChanged(object sender, System.EventArgs e)
		{
			lstTabs.DataSource = TabsList;
			lstTabs.DataBind();
		}
		protected void cmdAdd_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try {
				AddTab();
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected void cmdDelete_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try {
				DeleteTab();
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdEdit_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try {
				EditTab();
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdView_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try {
				ViewTab();
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
	}
}
